#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/mman.h>

#define LPC_EC_INDEX_PORT 0x2E
#define LPC_EC_DATA_PORT 0x2F

#define SIO_CONFIG_CTRL       0x02
#define SIO_CONFIG_PORT       0x2E

#define VOID       void
#define UINT8       unsigned char
#define UINT16       unsigned short

#define BIT1 0x02

unsigned char *get_virt_addr();
unsigned char linux_inb(unsigned short int port);
void linux_outb( unsigned short int port, unsigned char value);

unsigned char linux_inb(unsigned short int port)
{
	//printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
	//return inb(port);
	return (*(get_virt_addr()+port));
}
void linux_outb( unsigned short int port, unsigned char value)
{
	printf("%s %s %d; %p\n",__FILE__,__FUNCTION__,__LINE__,get_virt_addr()+port);
	*(get_virt_addr()+port) = value;
	//printf("%s %s %d\n",__FILE__,__FUNCTION__,__LINE__);
	//outb(value,port);
}

static ssize_t ecram_drv_config(unsigned char *ecram_vals);

static VOID EnterConfigMode ( VOID )
/*++
Routine Description:
  Enter Super IO Config Mode.
Arguments:
Returns:
  None
--*/  
{
  UINT8  PnpKey;

  PnpKey = 0x87;
  linux_outb(SIO_CONFIG_PORT, PnpKey);
  PnpKey = 0x01;
  linux_outb(SIO_CONFIG_PORT, PnpKey);
  PnpKey = 0x55;
  linux_outb(SIO_CONFIG_PORT, PnpKey);
  PnpKey = 0x55;
  linux_outb(SIO_CONFIG_PORT, PnpKey);
  
  return;
}

static VOID ExitConfigMode ( VOID )
{
  UINT8  RegData = 0x00;

  linux_outb (LPC_EC_INDEX_PORT, SIO_CONFIG_CTRL);
  RegData = linux_inb (LPC_EC_DATA_PORT) | BIT1;
  linux_outb (LPC_EC_INDEX_PORT, SIO_CONFIG_CTRL);  
  linux_outb (LPC_EC_DATA_PORT, RegData);
  
  return;
}

unsigned char *virt_addr=NULL;
unsigned char *get_virt_addr()
{
	return virt_addr;
}
/* seconddrvtest 
  */
int main(int argc, char **argv)
{
	int fd;
	unsigned char key_vals[256];
	unsigned char ec_line,ec_row,index_num;
	char *endstr;
	int val;

	off_t physical_addr = 0x90000efdfc000000;
	
	fd = open("/dev/mem", O_RDWR);
	if (fd < 0)
	{
		printf("can't open!\n");
		return -1;
	}
	printf("can open!\n");

	virt_addr = mmap(NULL, 0x100, PROT_READ | PROT_WRITE, MAP_SHARED, fd, physical_addr);

{
	int i;
	for(i=0;i<100;i++)
	{
		if(i%16 == 0)
			printf("\n");
		printf("%02X ",*(virt_addr+i));
	}
		
	printf("\n");

}

	if (virt_addr == MAP_FAILED) 
	{
		printf("virt_addr == MAP_FAILED\n");

			//打印出错信息

	}
	printf("virt_addr001 == %p\n",virt_addr);
	unsigned char ecram_vals[256];

	ecram_drv_config(ecram_vals);

	return 0;


	return 0;
}


static ssize_t ecram_drv_config(unsigned char *ecram_vals)
{
	unsigned char Chipid0,Chipid1 =0;
	UINT16  DataTemp;
	
	printf("test01 == %02X\n",0);
  EnterConfigMode();
  EnterConfigMode();
  printf("test01 == %02X\n",1);
  Chipid0=0;
  Chipid1=0;
  
  linux_outb (LPC_EC_INDEX_PORT, 0x20);  
  Chipid0 = linux_inb (LPC_EC_DATA_PORT);
  ecram_vals[0]=Chipid0;
  printf("Chipid0 == %02X\n",Chipid0);
  linux_outb (LPC_EC_INDEX_PORT, 0x21);  
  Chipid1 = linux_inb (LPC_EC_DATA_PORT); 
  ecram_vals[1]=Chipid1;
  printf("Chipid1 == %02X\n",Chipid1);

  ExitConfigMode();	
  return 0;
}

